CC = gcc
CFLAGS_HELPER = `pkg-config --cflags upower-glib gtk4 libadwaita-1 gio-2.0`
LDFLAGS_HELPER = -lwayland-client `pkg-config --libs upower-glib gtk4 libadwaita-1 gio-2.0`
CFLAGS_WRAPPERS = `pkg-config --cflags upower-glib`
LDFLAGS_WRAPPERS = `pkg-config --libs upower-glib` -lwayland-client
CFLAGS_GOVERNOR = `pkg-config --cflags upower-glib`
LDFLAGS_GOVERNOR = -lwayland-client `pkg-config --libs upower-glib`
CFLAGS_GUI = `pkg-config --cflags gtk4 libadwaita-1`
LDFLAGS_GUI = `pkg-config --libs gtk4 libadwaita-1`
CFLAGS_NFCD = -fPIC -DNFC_PLUGIN_EXTERNAL `pkg-config --cflags nfcd-plugin libglibutil gobject-2.0 glib-2.0`
LDFLAGS_NFCD = -fPIC -shared `pkg-config --libs libglibutil gobject-2.0 glib-2.0` -lwayland-client
CFLAGS_GBINDER = `pkg-config --cflags libgbinder`
LDFLAGS_GBINDER = `pkg-config --libs libgbinder`
CFLAGS_HYBRIS = `pkg-config --cflags libgbinder`
LDFLAGS_HYBRIS = `pkg-config --libs libgbinder`
CFLAGS_WIFI = `pkg-config --cflags glib-2.0 libnl-3.0 libnl-genl-3.0 libnl-route-3.0`
LDFLAGS_WIFI = `pkg-config --libs glib-2.0 libnl-3.0 libnl-genl-3.0 libnl-route-3.0`
CFLAGS_WAYDROID = `pkg-config --cflags gio-2.0`
LDFLAGS_WAYDROID = `pkg-config --libs gio-2.0`
CFLAGS_NICERDICER = `pkg-config --cflags gio-2.0`
LDFLAGS_NICERDICER = `pkg-config --libs gio-2.0`
CFLAGS_POWERCONFIG = `pkg-config --cflags gio-2.0`
LDFLAGS_POWERCONFIG = `pkg-config --libs gio-2.0`
CFLAGS_EXAMPLES = `pkg-config --cflags upower-glib`
LDFLAGS_EXAMPLES = `pkg-config --libs upower-glib` -lbatman-wrappers -lbatman-waydroid -lwayland-client

TARGET = batman
TARGET_HELPER = batman-helper
TARGET_GUI = batman-gui
TARGET_GOVERNOR = governor
TARGET_WRAPPERS = libbatman-wrappers.so
TARGET_GBINDER = libbatman-gbinder.so
TARGET_HYBRIS = batman-hybris
TARGET_NFCD = batman.so
TARGET_WIFI = batman-wifi
TARGET_BATMAN2PPD = src/batman2ppd.py
TARGET_PPDCLI = src/powerprofilesctl.py
TARGET_WAYDROID = libbatman-waydroid.so
TARGET_WAYDROID_CLI = batman-waydroid
TARGET_NICERDICER = batman-nicerdicer
TARGET_POWERCONFIG = batman-powerconfig
TARGET_EXAMPLES = batman-examples

SRC_HELPER = src/batman-helper.c src/wlrdisplay.c src/batman-wrappers.c src/getinfo.c src/batman-waydroid.c
SRC_GUI = src/batman-gui.c src/configcontrol.c src/getinfo.c
SRC_GOVERNOR = src/governor.c src/wlrdisplay.c src/batman-wrappers.c
SRC_WRAPPERS = src/batman-wrappers.c src/wlrdisplay.c src/getinfo.c
SRC_GBINDER = src/batman-gbinder.c
SRC_HYBRIS = src/batman-hybris.c src/batman-gbinder.c
SRC_NFCD = src/nfcd-batman-plugin.c src/wlrdisplay.c
SRC_WIFI = src/batman-wifi.c
SRC_WAYDROID = src/batman-waydroid.c
SRC_WAYDROID_CLI = src/batman-waydroid-cli.c src/batman-waydroid.c
SRC_NICERDICER = src/nicerdicer.c
SRC_POWERCONFIG = src/powerconfig.c
SRC_EXAMPLES = examples/batman-functions.c
HEADERS = src/batman-wrappers.h src/getinfo.h src/governor.h src/batman-gbinder.h src/batman-waydroid.h

PREFIX ?= /usr
LIBDIR ?= $(PREFIX)/lib
BINDIR ?= $(PREFIX)/bin
SBINDIR ?= $(PREFIX)/sbin
CONFIGDIR ?= /var/lib/batman
SYSTEMD_DIR ?= $(LIBDIR)/systemd/system
OPENRC_DIR ?= /etc/init.d
DESKTOP_DIR ?= $(PREFIX)/share/applications
ICON_DIR ?= $(PREFIX)/share/icons
INCLUDE_DIR ?= $(PREFIX)/include/batman
POLKIT_DIR ?= $(PREFIX)/share/polkit-1/actions
DBUS_DIR ?= $(PREFIX)/share/dbus-1/system.d

.PHONY: all
all: helper wrappers governor gui gbinder hybris wifi nfcd waydroid nicerdicer powerconfig

helper: $(TARGET_HELPER)
$(TARGET_HELPER):
	$(CC) $(SRC_HELPER) $(CFLAGS_HELPER) $(LDFLAGS_HELPER) -o $(TARGET_HELPER)

wrappers: $(TARGET_WRAPPERS)
$(TARGET_WRAPPERS):
	$(CC) -fPIC -shared $(SRC_WRAPPERS) $(CFLAGS_WRAPPERS) $(LDFLAGS_WRAPPERS) -o $(TARGET_WRAPPERS)

governor: $(TARGET_GOVERNOR)
$(TARGET_GOVERNOR):
	$(CC) $(SRC_GOVERNOR) $(LDFLAGS_GOVERNOR) $(CFLAGS_GOVERNOR) -o $(TARGET_GOVERNOR)

gui: $(TARGET_GUI)
$(TARGET_GUI):
	$(CC) $(SRC_GUI) $(CFLAGS_GUI) $(LDFLAGS_GUI) -o $(TARGET_GUI)

gbinder: $(TARGET_GBINDER)
$(TARGET_GBINDER):
	$(CC) -fPIC -shared $(SRC_GBINDER) $(CFLAGS_GBINDER) $(LDFLAGS_GBINDER) -o $(TARGET_GBINDER)

hybris: $(TARGET_HYBRIS)
$(TARGET_HYBRIS):
	$(CC) $(SRC_HYBRIS) $(CFLAGS_HYBRIS) $(LDFLAGS_HYBRIS) -o $(TARGET_HYBRIS)

wifi: $(TARGET_WIFI)
$(TARGET_WIFI):
	$(CC) $(SRC_WIFI) $(CFLAGS_WIFI) $(LDFLAGS_WIFI) -o $(TARGET_WIFI)

nfcd: $(TARGET_NFCD)
$(TARGET_NFCD): nfcd-batman-plugin.o wlrdisplay.o
	$(CC) $^ $(LDFLAGS_NFCD) -o $@

waydroid: $(TARGET_WAYDROID)
$(TARGET_WAYDROID):
	$(CC) -fPIC -shared $(SRC_WAYDROID) $(CFLAGS_WAYDROID) $(LDFLAGS_WAYDROID) -o $(TARGET_WAYDROID)
	$(CC) $(SRC_WAYDROID_CLI) $(CFLAGS_WAYDROID) $(LDFLAGS_WAYDROID)-o $(TARGET_WAYDROID_CLI)

nicerdicer: $(TARGET_NICERDICER)
$(TARGET_NICERDICER):
	$(CC) $(SRC_NICERDICER) $(CFLAGS_NICERDICER) $(LDFLAGS_NICERDICER) -o $(TARGET_NICERDICER)

powerconfig: $(TARGET_POWERCONFIG)
$(TARGET_POWERCONFIG):
	$(CC) $(SRC_POWERCONFIG) $(CFLAGS_POWERCONFIG) $(LDFLAGS_POWERCONFIG) -o $(TARGET_POWERCONFIG)

examples: $(TARGET_EXAMPLES)
$(TARGET_EXAMPLES):
	$(CC) $(SRC_EXAMPLES) $(CFLAGS_EXAMPLES) $(LDFLAGS_EXAMPLES) -o $(TARGET_EXAMPLES)

nfcd-batman-plugin.o: src/nfcd-batman-plugin.c
	$(CC) -c $< $(CFLAGS_NFCD) -O2 -o $@

wlrdisplay.o: src/wlrdisplay.c
	$(CC) -c $< $(CFLAGS_NFCD) -O2 -o $@

.PHONY: install
install: all
	cp src/$(TARGET) $(BINDIR)
	cp $(TARGET_HELPER) $(BINDIR)
	cp $(TARGET_GUI) $(BINDIR)
	cp $(TARGET_GOVERNOR) $(BINDIR)
	cp $(TARGET_WRAPPERS) $(LIBDIR)
	cp $(TARGET_GBINDER) $(LIBDIR)
	cp $(TARGET_HYBRIS) $(BINDIR)
	cp $(TARGET_WIFI) $(BINDIR)
	cp $(TARGET_BATMAN2PPD) $(BINDIR)/batman2ppd
	cp $(TARGET_PPDCLI) $(BINDIR)/powerprofilesctl
	cp $(TARGET_WAYDROID) $(LIBDIR)
	cp $(TARGET_WAYDROID_CLI) $(BINDIR)
	cp $(TARGET_NICERDICER) $(SBINDIR)
	cp $(TARGET_POWERCONFIG) $(SBINDIR)

	mkdir -p $(DESKTOP_DIR)
	cp data/batman-gui.desktop $(DESKTOP_DIR)

	mkdir -p $(ICON_DIR)
	cp data/batman.png $(ICON_DIR)

	mkdir -p $(CONFIGDIR)
	cp data/config $(CONFIGDIR)

	mkdir -p $(INCLUDE_DIR)
	cp $(HEADERS) $(INCLUDE_DIR)

	mkdir -p $(POLKIT_DIR)
	cp data/net.hadess.PowerProfiles.policy $(POLKIT_DIR)

	mkdir -p $(DBUS_DIR)
	cp data/net.hadess.PowerProfiles.conf $(DBUS_DIR)
	cp data/io.FuriOS.NicerDicer.conf $(DBUS_DIR)
	cp data/io.FuriOS.BatmanPowerConfig.conf $(DBUS_DIR)

ifeq ($(shell test -d $(SYSTEMD_DIR) && echo 1),1)
	cp data/batman.service $(SYSTEMD_DIR)
	cp data/batman2ppd.service $(SYSTEMD_DIR)
	cp data/nicerdicer.service $(SYSTEMD_DIR)
	cp data/powerconfig.service $(SYSTEMD_DIR)
else ifeq ($(shell test -e /sbin/openrc && echo 1),1)
	cp data/batman.rc $(OPENRC_DIR)/batman
else
	cp data/batman-init $(OPENRC_DIR)/batman
endif

	cp data/nicerdicer.conf /etc/nicerdicer.conf

.PHONY: clean
clean:
	rm -f $(TARGET_HELPER)
	rm -f $(TARGET_GUI)
	rm -f $(TARGET_GOVERNOR)
	rm -f $(TARGET_WRAPPERS)
	rm -f $(TARGET_GBINDER)
	rm -f $(TARGET_HYBRIS)
	rm -f $(TARGET_NFCD)
	rm -f $(TARGET_WIFI)
	rm -f $(TARGET_WAYDROID)
	rm -f $(TARGET_WAYDROID_CLI)
	rm -f $(TARGET_NICERDICER)
	rm -f $(TARGET_EXAMPLES)
	rm -f $(TARGET_POWERCONFIG)
	rm -f nfcd-batman-plugin.o wlrdisplay.o
